const mysql = require('../../mysql')

async function  return_superior(ctx) {
  const { id } = ctx.request.body

  try{
    await mysql('goods')
          .where({id})
          .update({state: 3})

    await mysql('goods_sold')
          .where({goods_id: id})
          .update({state: 0})

    ctx.state.data = {
      msg: '退货成功'
    }
  } catch (err) {
    console.log(err)
    ctx.state = {
      code: -1,
      data: {
        msg: '退货失败'
      }
    }
  }
}

async function  return_sale(ctx) {
  const { id } = ctx.request.body

  try{
    await mysql('goods')
          .where({id})
          .update({state: 1})

    await mysql('goods_sold')
          .where({goods_id: id})
          .update({state: 0})

    ctx.state.data = {
      msg: '退货成功'
    }
  } catch (err) {
    console.log(err)
    ctx.state = {
      code: -1,
      data: {
        msg: '退货失败'
      }
    }
  }
}

module.exports = {
  return_superior,
  return_sale
}